﻿Imports System.IO
Imports System.Xml.Serialization
Imports System.Collections.Generic
Imports Microsoft.Office.Interop
Imports System.Runtime.CompilerServices

Module MyGlobals
    'Public apikey As String = "f39a5f6e8bab4c720a2f273b84e45845"
    Dim SyncedItemsLocation As String = My.Computer.FileSystem.CurrentDirectory & "\SyncedItems.xml"
    'todo enter your shared secret key here
    Public sharedsecret As String = "enter your shared secret key here"

    Dim _SyncedItems As SyncedItemsList

    <Extension()> _
    Public Function IsRepeating(ByVal OutlookTask As Outlook.TaskItem) As Boolean
        Try
            Dim rec As Outlook.RecurrencePattern = OutlookTask.GetRecurrencePattern()
            Dim rec2 As Outlook.RecurrencePattern = OutlookTask.GetRecurrencePattern()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    <Extension()> _
    Public Function Contains(ByVal TaskCollection() As RTMnet.TaskSeries, ByVal TaskSeriesId As String) As Boolean
        Dim results = (From item In TaskCollection Where item.TaskSeriesID = TaskSeriesId).ToList
        If results.Count > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    <Extension()> _
    Public Function Contains(ByVal TaskSeries As List(Of RTMnet.TaskSeries), ByVal syncitem As SyncItem) As Boolean
        Dim results = (From series In TaskSeries Where series.TaskSeriesID = syncitem.TaskSeriesId AndAlso _
                        series.TaskInfo(0).ID = syncitem.TaskId).ToList
        If results.Count > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    <Extension()> _
  Public Function GetTaskSeries(ByVal TaskSeries As List(Of RTMnet.TaskSeries), ByVal TaskSeriesId As String) As RTMnet.TaskSeries
        Dim results = (From series In TaskSeries Where series.TaskSeriesID = TaskSeriesId).ToList
        If results.Count > 0 Then
            Return results(0)
        Else
            Return Nothing
        End If
    End Function

    <Extension()> _
    Public Sub DeleteTask(ByVal OutlookTasks As Outlook.Items, ByVal EntryId As String)
        Dim results = (From task As Outlook.TaskItem In OutlookTasks Where task.EntryID = EntryId).ToList
        If results.Count > 0 Then
            results(0).Delete()
        End If
    End Sub

    <Extension()> _
    Public Function GetTask(ByVal OutlookTasks As List(Of Outlook.TaskItem), ByVal EntryId As String) As Outlook.TaskItem
        For Each task In OutlookTasks
            Try
                If task.EntryID = EntryId Then
                    Return task
                End If
            Catch ex As Exception

            End Try
        Next
        Return Nothing
        'Dim results = (From task As Outlook.TaskItem In OutlookTasks Where task.EntryID = EntryId).ToList
        'If results.Count > 0 Then
        '    Return results(0)
        'Else
        '    Return Nothing
        'End If
    End Function
    <Extension()> _
    Public Function Contains(ByVal OutlookTasks As Outlook.Items, ByVal item As SyncItem) As Boolean
        Dim results = (From task As Outlook.TaskItem In OutlookTasks Where task.EntryID = item.OutlookId).ToList
        If results.Count > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    <Extension()> _
    Public Sub Delete(ByVal Items As SyncedItemsList, ByVal item As Outlook.TaskItem)
        Dim results = (From syncitem In Items Where syncitem.OutlookId = item.EntryID).ToList
        If results.Count > 0 Then
            Items.Remove(results(0))
        End If
    End Sub

    Public Sub SaveSyncedItems()
        'make sure it is not null
        If IsNothing(SyncedItems) Then
            LoadSyncedItems()
        End If
        Using objStreamWriter As New StreamWriter(SyncedItemsLocation)
            Dim x As New XmlSerializer(SyncedItems.GetType)
            x.Serialize(objStreamWriter, SyncedItems)
            objStreamWriter.Close()
        End Using
    End Sub

    Private Sub LoadSyncedItems()
        SyncedItems = New SyncedItemsList
        If IO.File.Exists(SyncedItemsLocation) Then
            'Deserialize text file to a new object.
            Dim x As New XmlSerializer(SyncedItems.GetType)
            Using objStreamReader As New StreamReader(SyncedItemsLocation)
                SyncedItems = x.Deserialize(objStreamReader)
                objStreamReader.Close()
            End Using
        End If
    End Sub
    Public Sub DeleteSyncedItems()
        Try
            If IO.File.Exists(SyncedItemsLocation) Then
                IO.File.Delete(SyncedItemsLocation)
            End If
            SyncedItems = New SyncedItemsList
        Catch ex As Exception
            'ignore file delete error
            MyLog.Log("Failed to delete SyncedItems.xml", ex)
        End Try
    End Sub

    Public Property SyncedItems() As SyncedItemsList
        Get
            If IsNothing(_SyncedItems) Then
                LoadSyncedItems()
            End If
            Return _SyncedItems
        End Get
        Private Set(ByVal value As SyncedItemsList)
            _SyncedItems = value
        End Set
    End Property
End Module

Public Module MyApp
    Private _application As Outlook.Application
    Public ReadOnly Property Application() As Outlook.Application
        Get
            If IsNothing(_application) Then
                _application = New Outlook.Application()
            End If
            Return _application
        End Get
    End Property
End Module
